﻿Imports System.Text.RegularExpressions

Public Class CustomTextBlock
    Inherits TextBlock

    Public Event Klik As EventHandler(Of EventArgs)
    Public Shared ReadOnly InlineCollectionProperty As DependencyProperty = DependencyProperty.Register("InlineCollection", GetType(String), GetType(CustomTextBlock), New PropertyMetadata(New PropertyChangedCallback(AddressOf CustomTextBlock.InlineChanged)))

    Private Shared Sub InlineChanged(ByVal sender As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)

        DirectCast(sender, CustomTextBlock).Inlines.Clear()

        Dim kelimeler = Split(e.NewValue, " ")
        For i = 0 To kelimeler.Length - 1
            If Regex.Match(kelimeler(i), "(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?").Success Then

                Dim x = New Hyperlink(New Run(kelimeler(i)))
                x.AddHandler(Hyperlink.ClickEvent, New RoutedEventHandler(AddressOf t_Click))
                x.ToolTip = kelimeler(i)
                x.Tag = kelimeler(i)
                DirectCast(sender, CustomTextBlock).Inlines.Add(x)
                If Not i = kelimeler.Length Then DirectCast(sender, CustomTextBlock).Inlines.Add(" ")
            Else
                DirectCast(sender, CustomTextBlock).Inlines.Add(kelimeler(i))
                If Not i = kelimeler.Length Then DirectCast(sender, CustomTextBlock).Inlines.Add(" ")
            End If
            'Console.WriteLine(kelime(i).ToString.StartsWith("@"))
        Next
        kelimeler = Nothing
        'DirectCast(sender, CustomTextBlock).Inlines.Clear()
        'DirectCast(sender, CustomTextBlock).Inlines.AddRange(inlines)
        'DirectCast(sender, CustomTextBlock).Inlines.Add(New Bold(New Run(e.NewValue)))

        'Console.WriteLine("11111" & e.NewValue)
        'Console.WriteLine(tb.InlineCollection.ToString)
    End Sub
    Public Property InlineCollection As String 'InlineCollection
        Get
            Return DirectCast(GetValue(InlineCollectionProperty), String)
        End Get
        Set(ByVal value As String)
            SetValue(InlineCollectionProperty, value)
        End Set
    End Property

    'Public Event ThreadSafeUpdate As EventHandler(Of UpdateEventArgs)

    Private Shared Sub t_Click(ByVal sender As Hyperlink, ByVal e As System.Windows.RoutedEventArgs)
        ' Handle the event.
        Console.WriteLine(sender.Tag)
        e.Handled = True
        cagir(sender, e)
        'x.Show()
        'System.Diagnostics.Process.Start(sender.Tag)
    End Sub
    Private Sub cagir(ByVal sender As Hyperlink, ByVal e As System.Windows.RoutedEventArgs)

    End Sub


End Class